package cn.picclife.mwx.manager.util;

import java.awt.Color;
import java.io.IOException;
import java.io.OutputStream;
import java.security.SecureRandom;
import java.util.Random;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.patchca.color.ColorFactory;
import org.patchca.filter.predefined.MarbleRippleFilterFactory;
import org.patchca.font.RandomFontFactory;
import org.patchca.service.ConfigurableCaptchaService;
import org.patchca.text.renderer.BestFitTextRenderer;
import org.patchca.text.renderer.TextRenderer;
import org.patchca.utils.encoder.EncoderHelper;
import org.patchca.word.RandomWordFactory;

/**
 * @author Levi.Wang
 * 验证码工具类
 */
public class Captcha {
	private static Log log= LogFactory.getLog(Captcha.class);
	private static final String DEFAULT_CHARACTERS = "0123456789"; // 验证码字符库
	private static final int DEFAULT_FONT_SIZE = 25; // 默认字体大小
	private static final int DEFAULT_WORD_LENGTH = 4; // 默认长度
	//private static final int DEFAULT_WIDTH = 80; // 生成图片大小宽度(像素)
	//private static final int DEFAULT_HEIGHT = 35; // 生成图片大小高度(像素)

	private static ConfigurableCaptchaService ccs = new ConfigurableCaptchaService();
	static {
		TextRenderer tr = new BestFitTextRenderer();
		// 生成的单词设置
		RandomWordFactory rwf = new RandomWordFactory();
		rwf.setCharacters(DEFAULT_CHARACTERS);
		rwf.setMaxLength(DEFAULT_WORD_LENGTH);
		rwf.setMinLength(DEFAULT_WORD_LENGTH);

		ColorFactory cf = new ColorFactory() {
			@Override
			public Color getColor(int x) {
				//Random random = new Random();
				int[] c = new int[3];
				try {
					SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
					int i = random.nextInt(c.length);
					for (int fi = 0; fi < c.length; fi++) {
						if (fi == i) {
							c[fi] = random.nextInt(71);
						} else {
							c[fi] = random.nextInt(256);
						}
					}
				} catch (Exception e) {
					log.error("异常信息"+e.getMessage(),e);
				}
				return new Color(c[0], c[1], c[2]);
			}
		};

		// 字体大小设置
		RandomFontFactory ff = new RandomFontFactory();
		ff.setRandomStyle(false);
		ff.setMaxSize(DEFAULT_FONT_SIZE);
		ff.setMinSize(DEFAULT_FONT_SIZE);

		// 处理器
		ccs.setTextRenderer(tr);
		// ccs.setFontFactory(ff);
		ccs.setWordFactory(rwf);
		ccs.setColorFactory(cf);

		// 生成图片大小（像素）
		// ccs.setWidth(DEFAULT_WIDTH);
		// ccs.setHeight(DEFAULT_HEIGHT);

		//CurvesRippleFilterFactory crff = new CurvesRippleFilterFactory(ccs.getColorFactory());// 干扰线波纹
		//DoubleRippleFilterFactory drff = new DoubleRippleFilterFactory(); // 双波纹
		//WobbleRippleFilterFactory wrff = new WobbleRippleFilterFactory(); // 摆波纹
		//DiffuseRippleFilterFactory dirff = new DiffuseRippleFilterFactory(); // 漫波纹
		MarbleRippleFilterFactory mrff = new MarbleRippleFilterFactory(); // 大理石波纹

		ccs.setFilterFactory(mrff);
	}

	
	/**
	 * 获得验证码文本并且将图片写入到输出流
	 * @param os 输出流
	 * @return 验证码文本
	 */
	public static String getCaptchaAndWriteImage(OutputStream os) {
		String captcha;
		try {
			captcha = EncoderHelper.getChallangeAndWriteImage(ccs, "png", os);
		} catch (IOException e) {
			throw new RuntimeException("验证码写入错误",e);
		}

		return captcha;
	}

}
